using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SourceSystemManager.Helper;
using System.Data.OleDb;
using System.IO;
using DoMain;
using System.Diagnostics;

namespace SourceSystemManager
{
    public partial class MainForm : Form
    {
        DBManager dbm = null;
        List<LinkInfo> linfos = new List<LinkInfo>();
        List<Label> lblList = new List<Label>();
        TabViewManager tabView;
        bool CloseAfterSaved = false;

        public MainForm()
        {
            InitializeComponent();
            string currentPath = Directory.GetCurrentDirectory();
            dbm = new DBManager(currentPath + "\\SSMDatabase.accdb");
            tabView = new TabViewManager(tabPage2, new Point(6, 127));
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            // input string is null or empty
            if (string.IsNullOrEmpty(txtInputString.Text))
            {
                MessageBoxEx.Show("No matched data avaiable!", "Not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // if input string is not null

        }

        void GetData(string data)
        {

        }

        private void txtInputString_KeyDown(object sender, KeyEventArgs e)
        {
            // press Enter key
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(sender, EventArgs.Empty);
            }
        }

        bool check = false;
        private void txtInputString_TextChanged(object sender, EventArgs e)
        {
            if (!check)
            {
                lMatchString.Visible = true;
                lMatchString.Height += lMatchString.ItemHeight;
            }
        }

        private void lMatchString_MouseHover(object sender, EventArgs e)
        {
            Point point = lMatchString.PointToClient(Cursor.Position);
            int index = lMatchString.IndexFromPoint(point);
            if (index < 0) return;
            //Do any action with the item
            lMatchString.GetItemRectangle(index).Inflate(1, 2);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnAddLink_Click(object sender, EventArgs e)
        {
            InsertLinkForm isf = new InsertLinkForm();
            isf.SendData += new SendData(isf_GetData);
            isf.ShowDialog();
        }

        void isf_GetData(string link, string type, string des)
        {
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!btnDeleteLink.Enabled && listBox1.SelectedIndex != -1)
                btnDeleteLink.Enabled = true;

            int pos = listBox1.SelectedIndex;
            if (pos != -1)
            {
                txtDescription.Text = linfos[pos].LinkDescription;
                lkLink.Text = linfos[pos].LinkName;
            }
        }

        private void btnDeleteLink_Click(object sender, EventArgs e)
        {
            if (MessageBoxEx.Show("Do you want to delete this link from database?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                dbm.pdbOpen();
                int curIndex = listBox1.SelectedIndex;
                listBox1.Items.RemoveAt(curIndex);
                txtDescription.Text = "";
                lkLink.Text = "";
                btnDeleteLink.Enabled = false;

                string query = string.Format("DELETE FROM Links WHERE ID = " + linfos[curIndex].LinkID.ToString() + "");
                dbm.pdbInsUpdDelDB(query);
            }
        }

        private void cbTypeFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    ocon.Open();
            //    listBox1.Items.Clear();
            //    linfos = new List<LinkInfo>();
            //}
            //catch (Exception ex)
            //{
            //    MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
            //try
            //{
            //    string vsql;
            //    if (cbTypeFilter.SelectedIndex == 0)
            //        vsql = string.Format("SELECT * FROM Links");
            //    else
            //        vsql = string.Format("SELECT * FROM Links WHERE Type = '" + cbTypeFilter.Text + "'");
            //    OleDbCommand vcom = new OleDbCommand(vsql, ocon);
            //    OleDbDataReader reader = vcom.ExecuteReader();
            //    while (reader.Read())
            //    {
            //        listBox1.Items.Add(reader["Title"].ToString());
            //        LinkInfo li = new LinkInfo(int.Parse(reader["ID"].ToString())     /*Link id*/,
            //                                   reader["Type"].ToString()     /*Link type*/,
            //                                   reader["Title"].ToString()     /*Link title*/,
            //                                   reader["Path"].ToString()     /*Link name*/,
            //                                   reader["Description"].ToString()     /*Link description*/);
            //        linfos.Add(li);

            //    }

            //    if (linfos.Count == 0)
            //    {
            //        lblStatus.Visible = true;
            //        txtDescription.Text = "";
            //        lkLink.Text = "";
            //    }
            //    else
            //        lblStatus.Visible = false;

            //    vcom.Dispose();
            //}
            //catch (Exception ex)
            //{
            //    MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
            //finally
            //{
            //    if (ocon != null)
            //        ocon.Close();
            //}
        }

        private void lkLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(lkLink.Text);
        }

        void sf_OnSendData(string file_name, string file_des)
        {
            tabView.SetName(file_name + ".txt");

            if (CloseAfterSaved)
            {
                tabView.CloseCurrentTab();
                CloseAfterSaved = false;
            }
        }

        private void btnSetLinkSource_Click(object sender, EventArgs e)
        {
            if (fldBrowserDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtLinkSource.Text = fldBrowserDlg.SelectedPath;
            }
        }

        private void btnSetDataSource_Click(object sender, EventArgs e)
        {
            if (fldBrowserDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtDataSource.Text = fldBrowserDlg.SelectedPath;
            }
        }

        private void btnNewData_Click(object sender, EventArgs e)
        {
            if (tabView.Full())
                return;

            var txt = new TextBox();
            txt.Multiline = true;
            txt.KeyDown += txt_KeyDown;
            txt.TextChanged += txt_TextChanged;
            txt.Size = new System.Drawing.Size(803, 357);

            var lbl = new Label();
            lbl.AutoSize = false;
            lbl.BackColor = Color.Orange;
            lbl.Size = new Size(tabView.MaxLengthHeader, 15);
            lbl.Text = tabView.DefaultName;

            tabView.AddNewTab(lbl, txt);
        }

        void txt_TextChanged(object sender, EventArgs e)
        {
            switch (tabView.ActiveTab.Status)
            {
                case TabStatus.stNEW_DOCUMENT:
                    tabView.NameChanged = true;
                    tabView.SetStatus(TabStatus.stNEW_DOCUMENT_MODIFIED);
                    break;
                case TabStatus.stNEW_DOCUMENT_SAVED:
                    tabView.NameChanged = true;
                    tabView.SetStatus(TabStatus.stNEW_DOCUMENT_SAVED_MODIFIED);
                    break;
                case TabStatus.stOPEN_DOCUMENT:
                case TabStatus.stOPEN_DOCUMENT_SAVED:
                    tabView.SetStatus(TabStatus.stOPEN_DOCUMENT_MODIFIED);
                    break;
                case TabStatus.NONE:
                    break;
                default:
                    break;
            }
        }

        void txt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.S)
            {
                if (tabView.ActiveTab.Status == TabStatus.stNEW_DOCUMENT ||
                    tabView.ActiveTab.Status == TabStatus.stNEW_DOCUMENT_MODIFIED)
                {
                    SaveDataForm sf = new SaveDataForm();
                    sf.SendDataInfo += new SendDataInfo(sf_OnSendData);
                    sf.ShowDialog();
                    e.SuppressKeyPress = true;
                }
                else
                {
                    // save modified content of the data file here
                }
            }
            else if (e.Control && e.KeyCode == Keys.W)
            {
                if (tabView.ActiveTab.Status == TabStatus.stNEW_DOCUMENT_MODIFIED ||
                    tabView.ActiveTab.Status == TabStatus.stOPEN_DOCUMENT_MODIFIED)
                {
                    if (MessageBoxEx.Show("Do you want to save ?", "Save File", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
                    {
                        CloseAfterSaved = true;
                        SaveDataForm sf = new SaveDataForm();
                        sf.SendDataInfo += new SendDataInfo(sf_OnSendData);
                        sf.ShowDialog();
                        e.SuppressKeyPress = true;
                    }
                    else
                        tabView.CloseCurrentTab();
                }
                else
                    tabView.CloseCurrentTab();
            }
        }

        private void btnOpenData_Click(object sender, EventArgs e)
        {
            FileContainerForm fcf = new FileContainerForm();
            fcf.ShowDialog();
            //if (opFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    if (index > tabView.MaxTab - 1)
            //        return;

            //    var txt = new TextBox();
            //    txt.Multiline = true;
            //    txt.KeyDown += txt_KeyDown;
            //    txt.Size = new System.Drawing.Size(803, 357);
            //    txt.Location = new Point(6, 127);

            //    var lbl = new Label();
            //    lbl.AutoSize = false;
            //    lbl.MouseDown += lbl_MouseDown;
            //    lbl.BackColor = Color.Orange;
            //    lbl.TabIndex = index;
            //    lbl.Size = new Size(100, 15);
            //    lbl.Location = new Point(xx, 112);

            //    if (tabView.AddNewTab(lbl, txt))
            //    {
            //        tabView.SetName(opFileDlg.SafeFileName);
            //        xx += lbl.Size.Width + 2;
            //        index++;
            //    }
            //}
        }
    }
}

